package com.example.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.entity.SensitiveWord;
import com.example.enums.StatusEnum;
import com.example.service.SensitiveWordBsService;
import com.example.service.SensitiveWordService;
import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
import com.github.houbb.sensitive.word.support.allow.WordAllows;

@Service
class SensitiveWordBsServiceImpl implements SensitiveWordBsService {

    @Autowired
    private SensitiveWordBs sensitiveWordBs;

    @Override
    public void refresh() {
        sensitiveWordBs.init();
    }

    @Override
    public List<String> findInText(String text) {
        return sensitiveWordBs.findAll(text);
    }

    @Override
    public void add(SensitiveWord sw) {
        if (sw.getStatus() == StatusEnum.DENY) {
            sensitiveWordBs.addWord(sw.getWord());
        } else if (sw.getStatus() == StatusEnum.ALLOW) {
            sensitiveWordBs.addWordAllow(sw.getWord());
        }
    }

    @Override
    public void remove(SensitiveWord sw) {
        if (sw.getStatus() == StatusEnum.DENY) {
            sensitiveWordBs.removeWord(sw.getWord());
        } else if (sw.getStatus() == StatusEnum.ALLOW) {
            sensitiveWordBs.removeWordAllow(sw.getWord());
        }
    }
}